package com.jiangjiang.eshop.auth.operation;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.jiangjiang.eshop.auth.domain.PriorityEntity;
import com.jiangjiang.eshop.auth.dto.PriorityNode;
import com.jiangjiang.eshop.auth.service.PriorityService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName PriorityNodeRemoveVisitor
 * @Description 权限树删除访问者
 * @Author jhw
 * @Date 2021/5/8 11:21
 * @Version 1.0
 **/
@Component
@Scope("prototype")
public class PriorityNodeRemoveOperation implements PriorityOperation<Boolean> {

    /**
     * 权限管理模块的门面类
     */
    @Autowired
    private PriorityService priorityService;

    @Override
    public Boolean doExecute(PriorityNode node) throws Exception {
        List<PriorityEntity> entities = priorityService.listChildrenPriorities(node.getId());
        if(CollectionUtils.isNotEmpty(entities)) {
            for(PriorityEntity entity : entities) {
                PriorityNode priorityNode = new PriorityNode();
                BeanUtils.copyProperties(entity,priorityNode);
                priorityNode.execute(this);
            }
        }

        // 删除权限
        removePriority(node);
        return true;
    }


    /**
     * 删除权限
     * @param node 权限节点
     */
    public void removePriority(PriorityNode node) {
         priorityService.removePriority(node.getId());
    }


}
